package com.glodon.paas.monitor.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.monitor.server.biz.domain.MonitorAlarmRulesEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorApplicationEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorItemEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorPluginEntity;
import com.glodon.paas.monitor.server.biz.service.MonitorPluginService;
import com.glodon.paas.monitor.server.constants.MonitorStatusEnum;
import com.glodon.paas.monitor.server.dto.MonitorAlarmRulesDTO;
import com.glodon.paas.monitor.server.dto.MonitorApplicationUserDTO;
import com.glodon.paas.monitor.server.dto.MonitorItemDTO;
import com.glodon.paas.monitor.server.dto.MonitorResultMsg;
import com.glodon.paas.monitor.server.exposed.facade.MonitorAppUserFacadeService;
import com.glodon.paas.monitor.server.exposed.facade.MonitorItemAlarmSendFacadeService;
import com.glodon.paas.monitor.web.constants.MonitorWebPageConstants;

@Controller
public class MonitorAlarmController {

	@Autowired
	private MonitorItemAlarmSendFacadeService	monitorItemAlarmSendFacadeService;

	@Autowired
	private MonitorAppUserFacadeService			monitorAppUserFacadeService;

	@Autowired
	private MonitorPluginService				monitorPluginService;

	@RequestMapping(value = "alarmMain", method = RequestMethod.GET)
	public ModelAndView indexAlarmMain(@RequestParam(value = "appId", required = false) Long appId,
			@RequestParam(value = "pluginId", required = false) Long pluginId,
			@RequestParam(value = "itemId", required = false) Long itemId,
			@RequestParam(value = "alarmName", required = false) String alarmName,
			@RequestParam(value = "alarmStatus", required = false) String alarmStatus, ModelMap model,
			HttpServletRequest request) {

		boolean modelFlag = false;
		Long alarmId = null;
		ModelMap doAlarmModel = (ModelMap) request.getSession().getAttribute("doAlarmModel");
		if (null != doAlarmModel && !doAlarmModel.isEmpty()) {
			modelFlag = true;
			alarmId = (Long) doAlarmModel.get(StringConst.ITEM);
		}

		MonitorAlarmRulesDTO alarmDto = new MonitorAlarmRulesDTO();
		if (null != appId) {
			alarmDto.setAppId(appId);
		}

		if (null != pluginId) {
			alarmDto.setPluginId(pluginId);
		}

		if (null != itemId) {
			alarmDto.setItemId(itemId);
		}

		if (null != alarmId) {
			alarmDto.setId(alarmId);
		}

		if (StringUtils.isNotBlank(alarmName)) {
			alarmDto.setAlarmName(alarmName);
		}

		if (StringUtils.isNotBlank(alarmStatus)) {
			alarmDto.setAlarmStatus(alarmStatus);
		}

		MonitorResultMsg<ArrayList<MonitorAlarmRulesEntity>> resultMsg = monitorItemAlarmSendFacadeService
				.queryMonitorAlarm(alarmDto);

		if (null != resultMsg) {
			if (resultMsg.isSuccess()) {
				ArrayList<MonitorAlarmRulesEntity> itemList = (ArrayList<MonitorAlarmRulesEntity>) resultMsg
						.getResultObject();
				List<MonitorAlarmRulesDTO> alarmRulesDTOList = new ArrayList<MonitorAlarmRulesDTO>();
				if (null != itemList && !itemList.isEmpty()) {
					for (MonitorAlarmRulesEntity entity : itemList) {
						MonitorAlarmRulesDTO alarmRulesDTO = new MonitorAlarmRulesDTO();
						alarmRulesDTO.setAlarmInterval(entity.getAlarmInterval());
						alarmRulesDTO.setAlarmLevel(entity.getAlarmLevel());
						alarmRulesDTO.setAlarmName(entity.getAlarmName());
						alarmRulesDTO.setAlarmRules(entity.getAlarmRules());
						alarmRulesDTO.setAlarmStatus(entity.getAlarmStatus());
						alarmRulesDTO.setAlarmTimes(entity.getAlarmTimes());
						alarmRulesDTO.setAppId(entity.getAppId());
						alarmRulesDTO.setCreateTime(entity.getCreateTime());
						alarmRulesDTO.setId(entity.getId());
						alarmRulesDTO.setItemId(entity.getItemId());

						alarmRulesDTO.setPluginId(entity.getPluginId());
						MonitorResultMsg<MonitorApplicationUserDTO> appUserMsg = (MonitorResultMsg<MonitorApplicationUserDTO>) monitorAppUserFacadeService
								.findMonitorApplicationUser(entity.getAppId());
						if (null != appUserMsg && appUserMsg.isSuccess()) {
							MonitorApplicationUserDTO appUserDTO = (MonitorApplicationUserDTO) appUserMsg
									.getResultObject();
							MonitorApplicationEntity appEntity = appUserDTO.getMonitorApplicationEntity();
							if (null != appEntity) {
								alarmRulesDTO.setAppName(appEntity.getAppName());
							}
						}
						MonitorPluginEntity pluginEntity = monitorPluginService
								.selectByPrimaryKey(entity.getPluginId());
						if (null != pluginEntity) {
							alarmRulesDTO.setPluginName(pluginEntity.getPluginName());
						}
						MonitorResultMsg<MonitorItemEntity> itemMsg = (MonitorResultMsg<MonitorItemEntity>) monitorItemAlarmSendFacadeService
								.findMonitorItemById(entity.getItemId());
						if (null != itemMsg && itemMsg.isSuccess()) {
							MonitorItemEntity itemEntity = (MonitorItemEntity) itemMsg.getResultObject();
							if (null != itemEntity) {
								alarmRulesDTO.setItemName(itemEntity.getItemName());
							}
						}
						alarmRulesDTOList.add(alarmRulesDTO);
					}
				}
				model.addAttribute(StringConst.ITEMS, alarmRulesDTOList);
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.QUERY_RESULT_EMPTY);
		}

		if (modelFlag) {
			model.addAttribute(StringConst.RETURN_MSG, doAlarmModel.get(StringConst.RETURN_MSG));
			model.addAttribute(StringConst.RESULT, doAlarmModel.get(StringConst.RESULT));
			request.getSession().removeAttribute("doAlarmModel");
		}
		return new ModelAndView("alarmMain", model);
	}

	@RequestMapping(value = "alarmAction", method = RequestMethod.GET)
	public ModelAndView indexAppAction(@RequestParam(value = "id", required = false) Long id, ModelMap model) {

		MonitorApplicationEntity entity = new MonitorApplicationEntity();
		MonitorResultMsg<ArrayList<MonitorApplicationEntity>> entityrReslut = monitorAppUserFacadeService
				.queryMonitorApplication(entity);
		if (null != entityrReslut) {
			if (entityrReslut.isSuccess()) {
				ArrayList<MonitorApplicationEntity> entityList = entityrReslut.getResultObject();
				model.addAttribute(MonitorWebPageConstants.APP_LIST, entityList);
			}
		}

		List<MonitorPluginEntity> pluginList = monitorPluginService.listAllPlugin();
		if (null != pluginList && !pluginList.isEmpty()) {
			model.addAttribute(MonitorWebPageConstants.PLUGIN_LIST, pluginList);
		}

		MonitorItemDTO itemEntity = new MonitorItemDTO();
		MonitorResultMsg<ArrayList<MonitorItemDTO>> itemResultMsg = monitorItemAlarmSendFacadeService
				.queryMonitorItem(itemEntity);
		if (null != itemResultMsg) {
			if (itemResultMsg.isSuccess()) {
				ArrayList<MonitorItemDTO> itemList = itemResultMsg.getResultObject();
				model.addAttribute(MonitorWebPageConstants.ITEM_LIST, itemList);
			}
		}

		MonitorResultMsg<MonitorAlarmRulesEntity> resultMsg = monitorItemAlarmSendFacadeService
				.findMonitorAlarmRulesById(id);
		if (null != resultMsg && resultMsg.isSuccess()) {
			MonitorAlarmRulesEntity alarmEntity = (MonitorAlarmRulesEntity) resultMsg.getResultObject();
			if (null != alarmEntity) {
				model.addAttribute("alarmEntity", alarmEntity);
			}
		}
		model.addAttribute("alarmId", id);
		return new ModelAndView("alarmAction", model);
	}

	@RequestMapping(value = "doAlarmAction", method = RequestMethod.POST)
	public ModelAndView doAppAction(@RequestParam(value = "alarmId", required = false) Long alarmId,
			@RequestParam(value = "appId", required = false) Long appId,
			@RequestParam(value = "pluginId", required = false) Long pluginId,
			@RequestParam(value = "itemId", required = false) Long itemId,
			@RequestParam(value = "alarmName", required = false) String alarmName,
			@RequestParam(value = "alarmLevel", required = false) String alarmLevel,
			@RequestParam(value = "warningTimes", required = false) Integer warningTimes,
			@RequestParam(value = "alarmInterval", required = false) Integer alarmInterval,
			@RequestParam(value = "alarmTimes", required = false) Integer alarmTimes,
			@RequestParam(value = "lastAlarmTime", required = false) String lastAlarmTime,
			@RequestParam(value = "alarmRules", required = false) String alarmRules, ModelMap model,
			HttpServletRequest request) {
		if (null != alarmId) {
			MonitorResultMsg<MonitorAlarmRulesEntity> resultMsg = monitorItemAlarmSendFacadeService
					.findMonitorAlarmRulesById(alarmId);
			if (null != resultMsg && resultMsg.isSuccess()) {
				MonitorAlarmRulesEntity alarmRulesEntity = (MonitorAlarmRulesEntity) resultMsg.getResultObject();
				alarmRulesEntity.setUpdateTime(new Date());
				alarmRulesEntity.setAlarmInterval(alarmInterval);
				alarmRulesEntity.setAlarmLevel(alarmLevel);
				alarmRulesEntity.setAlarmName(alarmName);
				alarmRulesEntity.setAlarmRules(alarmRules);
				alarmRulesEntity.setAlarmTimes(alarmTimes);
				alarmRulesEntity.setWarningTimes(warningTimes);
				alarmRulesEntity.setAppId(appId);
				alarmRulesEntity.setItemId(itemId);
				alarmRulesEntity.setPluginId(pluginId);
				MonitorResultMsg<MonitorAlarmRulesEntity> updateResultMsg = monitorItemAlarmSendFacadeService
						.updateMonitorAlarmRules(alarmRulesEntity);
				model.addAttribute(StringConst.ITEM, alarmId);

				if (null != updateResultMsg) {
					if (resultMsg.isSuccess()) {
						model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
						model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.UPDATE_SUCCESS);
					} else {
						model.addAttribute(StringConst.RESULT, StringConst.FAIL);
						model.addAttribute(StringConst.RETURN_MSG, updateResultMsg.getResultMsg());
					}
				} else {
					model.addAttribute(StringConst.RESULT, StringConst.FAIL);
					model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.UPDATE_FAIL);
				}
			}
		} else {
			MonitorAlarmRulesEntity alarmRulesEntity = new MonitorAlarmRulesEntity();
			alarmRulesEntity.setAlarmStatus(MonitorStatusEnum.ON.getCode());
			alarmRulesEntity.setCreateTime(new Date());
			alarmRulesEntity.setAlarmInterval(alarmInterval);
			alarmRulesEntity.setAlarmLevel(alarmLevel);
			alarmRulesEntity.setAlarmName(alarmName);
			alarmRulesEntity.setAlarmRules(alarmRules);
			alarmRulesEntity.setAlarmTimes(alarmTimes);
			alarmRulesEntity.setWarningTimes(warningTimes);
			alarmRulesEntity.setAppId(appId);
			alarmRulesEntity.setItemId(itemId);
			alarmRulesEntity.setPluginId(pluginId);
			MonitorResultMsg<MonitorAlarmRulesEntity> resultMsg = monitorItemAlarmSendFacadeService
					.saveMonitorAlarmRules(alarmRulesEntity);
			if (null != resultMsg) {
				if (resultMsg.isSuccess()) {
					model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
					model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CREATE_SUCCESS);
					MonitorAlarmRulesDTO monitorAlarmRulesDTO = new MonitorAlarmRulesDTO();
					monitorAlarmRulesDTO.setAlarmName(alarmName);
					monitorAlarmRulesDTO.setAlarmStatus(alarmRulesEntity.getAlarmStatus());
					monitorAlarmRulesDTO.setAppId(appId);
					monitorAlarmRulesDTO.setItemId(itemId);
					monitorAlarmRulesDTO.setPluginId(pluginId);
					MonitorResultMsg<ArrayList<MonitorAlarmRulesEntity>> alarmMsg = monitorItemAlarmSendFacadeService
							.queryMonitorAlarm(monitorAlarmRulesDTO);
					if (null != alarmMsg && alarmMsg.isSuccess()) {
						ArrayList<MonitorAlarmRulesEntity> alarmList = (ArrayList<MonitorAlarmRulesEntity>) alarmMsg
								.getResultObject();
						if (null != alarmList && !alarmList.isEmpty()) {
							model.addAttribute(StringConst.ITEM, alarmList.get(0).getId());
						}
					}
				} else {
					model.addAttribute(StringConst.RESULT, StringConst.FAIL);
					model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
				}
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CREATE_FAIL);
			}
		}
		request.getSession().setAttribute("doAlarmModel", model);
		return new ModelAndView(new RedirectView("alarmMain"));
	}

	@RequestMapping(value = "deleteAlarm", method = RequestMethod.GET)
	public ModelAndView doAppDelete(@RequestParam("id") Long id, ModelMap model, HttpServletRequest request) {
		if (null == id) {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.INVALID_PARAMETER);
			request.getSession().setAttribute("doAlarmModel", model);
			return new ModelAndView(new RedirectView("alarmMain"));
		}
		MonitorResultMsg<Boolean> resultMsg = monitorItemAlarmSendFacadeService.deleteMonitorAlarmRulesById(id);
		if (null != resultMsg) {
			if (resultMsg.isSuccess()) {
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.DELETE_SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.DELETE_FAIL);
		}
		request.getSession().setAttribute("doAlarmModel", model);
		return new ModelAndView(new RedirectView("alarmMain"));
	}

	@RequestMapping(value = "changeStatusAlarm", method = RequestMethod.GET)
	public ModelAndView doAlarmChangeStatus(@RequestParam("id") Long id,
			@RequestParam("alarmStatus") String alarmStatus, ModelMap model, HttpServletRequest request) {
		if (null == id || StringUtils.isBlank(alarmStatus)) {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.INVALID_PARAMETER);
			request.getSession().setAttribute("doAlarmModel", model);
			return new ModelAndView(new RedirectView("alarmMain"));
		}

		MonitorResultMsg<MonitorAlarmRulesEntity> resultMsg = monitorItemAlarmSendFacadeService
				.changeMonitorAlarmStatus(id, alarmStatus);
		if (null != resultMsg) {
			if (resultMsg.isSuccess()) {
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CHANGE_SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CHANGE_FAIL);
		}
		model.addAttribute(StringConst.ITEM, id);
		request.getSession().setAttribute("doAlarmModel", model);
		return new ModelAndView(new RedirectView("alarmMain"));
	}
}
